package com.hdjk.view.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.hdjk.view.config.Constant;

import org.json.JSONArray;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

public class StringAppUtil {

    public static String formatThumbImgUrl(String imgUrl) {
        if (!TextUtils.isEmpty(imgUrl)) {
            try {
                int imgNameIndex = imgUrl.lastIndexOf("/") + 1;
                String imageName = imgUrl.substring(imgNameIndex);
                if (imageName.contains("thumb_")) return imgUrl;
                String thumbImgUrl = imgUrl.substring(0, imgNameIndex) + "thumb_" + imageName;
                return thumbImgUrl;
            } catch (Exception e) {
                return imgUrl;
            }
        } else {
            return imgUrl;
        }
    }

    /**
     * 拆分图片路径
     *
     * @param originalList
     * @param thumbList
     */
    public static void getRealImgPath(String imgUrls, List<String> originalList, List<String> thumbList) {
        if (TextUtils.isEmpty(imgUrls) || originalList == null
                || thumbList == null) {
            return;
        }

        try {
            JSONArray tempArray = new JSONArray(imgUrls);
            if (tempArray != null && tempArray.length() > 0) {
                int count = tempArray.length();
                for (int i = 0; i < count; i++) {
                    String original = tempArray.getString(i);
                    if (!TextUtils.isEmpty(original)) {
                        if (!original.startsWith(Constant.NEW_IMG_URL)) {
                            original = Constant.NEW_IMG_URL + original;
                        }
                        originalList.add(original);

                        String thumb = formatThumbImgUrl(original);
                        thumbList.add(thumb);
                    }
                }
            }
        } catch (Exception e) {
        }
    }

    public static String formatDecimal(double value, int digits) {

        try {
            String strFormat = "#######0.";

            for (int i = 0; i < digits; i++) {
                strFormat += "0";
            }

            java.text.DecimalFormat df = new java.text.DecimalFormat(strFormat);
            return df.format(value);
        } catch (Exception e) {
            return String.valueOf(value);
        }
    }

    public static String formatDecimal(String value, int digits) {

        try {
            double valueD = Double.valueOf(value);

            String strFormat = "#######0.";

            for (int i = 0; i < digits; i++) {
                strFormat += "0";
            }

            java.text.DecimalFormat df = new java.text.DecimalFormat(strFormat);
            return df.format(valueD);
        } catch (Exception e) {
            return value;
        }
    }

    public static boolean isEmptyOrNull(String content) {
        if (TextUtils.isEmpty(content))
            return true;
        if ("null".equals(content))
            return true;
        return false;
    }

    /**
     * 对外提供getMD5(String)方法
     * @return
     */
    @SuppressLint("DefaultLocale")
    public static String getMD5(String val) {
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(val.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : result) {
                int number = b & 0xff;
                String hex = Integer.toHexString(number);
                if (hex.length() == 1) {
                    sb.append("0" + hex);
                } else {
                    sb.append(hex);
                }
            }
            return sb.toString().toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String getMerchantNumber(){
        int min=10000;
        int max=99999;
        Random random=new Random();
        int s = random.nextInt(max)%(max-min+1) + min;
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String today= year + "" + (month > 9 ? month : ("0" + month)) + "" + (day > 9 ? day : ("0" + day));
        return "ZHD"+today+s;
    }
}
